<?php

class UserController extends MSA_Controller_Action
{
	/**
	 * 
	 * 用户个人信息
	 */
    public function indexAction()
    {	
    	$uid = $this->getUid();
    	$modelUserInfo = new MSA_Model_AdminUserInfo();
       	if ($this->isPost()) {
       		$name = $this->getParam('name', '');
       		$email = $this->getParam('email', '');
       		$id = $this->getParam('id');
       		
       		if (!filter_var($id, FILTER_VALIDATE_INT)) {
       			throw new MSA_Exception('hack attemp!');
       		}
       		
       		if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       			throw new MSA_Exception('电子邮件地址不正确');
       		}
       		
       		/* 更新用户资料 */
       		$flag = $modelUserInfo->updateUserInfo($id, array('name' => $name, 'email' => $email));
       		if (!$flag) {
       			throw new MSA_Exception('更新个人信息失败，稍后再试!');
       		}
       		
       		/* 跳转到成功页面  */
       		$url = $this->generateUrl('index', 'user');
       		$this->view->redirectUrl = $url;
       		$this->renderScript('success/success.phtml');
       	}
       	else {
    		$info = $modelUserInfo->selectUserInfo($uid);
    		$this->view->userInfo = $info;
       	}
    }
    
    /**
     * 
     * 用户登录
     */
    public function loginAction()
    {
    	if ($this->isLogin()) {
    		$this->_redirect('/');
    	}	
    	
    	$this->setLayout('login');//设置layout
    	
    	if ($this->isPost()) {
	    	$captcha = $this->getParam('captcha', '');
	    	if (!$this->checkCaptcha($captcha)) throw new MSA_Exception('验证码不正确');
	    	
	    	$username = $this->getParam('username', '');
	    	$password = $this->getParam('password', '');
	    	
	    	if (empty($username) || empty($password)) {
	    		throw new MSA_Exception('用户名或密码不正确');
	    	}
	    	
	    	$dbAdapter = Zend_Db_Table::getDefaultAdapter();
	    	$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter, 'admin_user', 'username', 'password', 'md5(?)');
	    	$authAdapter->setIdentity($username);
	    	$authAdapter->setCredential($password);
	    	$result = $authAdapter->authenticate();
	    	
	    	if (!$result->isValid()) {
	    		throw new MSA_Exception('用户名或密码不正确');
	    	}
	    	
	    	$auth = $this->getAuth();
	    	$storage = $auth->getStorage();
	    	$storage->write( $authAdapter->getResultRowObject( array('id', 'role_id', 'username') ) );
	    	
	    	//记录登录时间
	    	$id = $auth->getIdentity()->id;
	    	$adminUser = new MSA_Model_AdminUser();
	    	$adminUser->updateLoginTime($id);
	    	
	    	//生成token cookie
	    	$token = base64_encode(time());
	    	$this->sendClientCookie('token', $token, time() + 604800, '/');
	    	$this->sendClientCookie('uid', $id, time() + 604800, '/');
	    	$adminUser->updateToken($id, $token);
	    	
	    	//完成，跳转到首页
	    	$this->_redirect('/');
    	}
    }
    
    public function logoutAction()
    {
    	$this->_helper->viewRenderer->setNoRender();//不输出模板
    	
    	if (!$this->isLogin()) {
    		throw new MSA_Exception('你还没登录!');
    	}
    	
    	/* 移除token */
    	$modelUser = new MSA_Model_AdminUser();
    	$modelUser->updateToken($this->getUid(), '');
    	
    	$this->getAuth()->clearIdentity();
    	
    	//完成，跳转到首页
    	$this->_redirect('/');
    }
    
    /**
     * 
     * 修改密码
     */
    public function passwordAction()
    {
    	$uid = $this->getUid();
    	
    	if ($this->isPost()) {
    		$oldPassword = $this->getParam('old-password', '');
    		$newPassword = $this->getParam('new-password', '');
    		$reNewPassword = $this->getParam('re-new-password', '');
    		
    		if (empty($oldPassword) || empty($newPassword) || empty($reNewPassword)) {
    			throw new MSA_Exception('必填项不能为空!');
    		}
    		
    		if ($newPassword !== $reNewPassword) {
    			throw new MSA_Exception('两次输入的新密码不一致');
    		}
    		
    		$modelUser = new MSA_Model_AdminUser();
    		try {
    			$modelUser->changePassword($uid, $oldPassword, $newPassword);
    		}
    		catch (MSA_Model_Exception $ex) {
    			throw new MSA_Exception($ex->getMessage());
    		}
    		
    		/* 跳转到成功页面  */
       		$url = $this->generateUrl('logout', 'user');
       		$this->view->redirectUrl = $url;
       		$this->renderScript('success/success.phtml');
    	}
    	else {
    		$modelUserInfo = new MSA_Model_AdminUserInfo();
    		$info = $modelUserInfo->selectUserInfo($uid);
    		$this->view->userInfo = $info;
    	}
    }


}

